      
# import os
# import shutil
# import cv2
# from PIL import Image
# import numpy as np
# import copy


# # 可移除异物：< gray_limit_canremove
# # 污染：> gray_limit_wuran
# gray_limit_canremove = 30
# gray_limit_wuran = 60
# gray_area_limit = 100
# # 灰点：> ash_limit 值越大，更多的灰点会判为黑点，值越小，更多的黑点会判为灰点
# ash_limit = 50

# # 1:pollution, seg_model
# seg_target_label = 1
# # 0:pollution det_model
# det_target_label = [0, 3, 5]
# # 新的可移除异物的标签
# can_remove = 0
# # 新的污染的标签
# new_pollution = 3

# # A temporary directory to save split images
# temp_split_images = 'temp_split_images'
# # A temporary directory to save split seg images
# temp_split_seg_images = 'temp_split_seg_images'
# # ori image shape
# ori_image_shape = [1380, 1036]
# # extend pixel
# extend_pixel = 3

# is_print = False

# glob_num = 0


# if not os.path.exists(temp_split_seg_images):
#     os.makedirs(temp_split_seg_images)
# else:
#     shutil.rmtree(temp_split_seg_images)
#     os.makedirs(temp_split_seg_images)

# def save_seg_images(mask, bbox_img):
#     global glob_num

#     mask = mask.astype(np.uint8)
#     mask[mask != 0] = 1
#     new_bbox_img = copy.deepcopy(bbox_img)
#     contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#     cv2.drawContours(new_bbox_img, contours, -1, (0, 255, 0), 1)
#     filename = os.path.join(temp_split_seg_images, f'{glob_num}.jpeg')
#     glob_num += 1
#     cv2.imwrite(filename, new_bbox_img)

# def save_split_images(result, image):
#     split_images_list = []
#     if not os.path.exists('temp_split_images'):
#         os.makedirs('temp_split_images')
#     else:
#         shutil.rmtree('temp_split_images')
#         os.makedirs('temp_split_images')
#     num = 0
#     for single_bbox_infos in result:
#         bbox = single_bbox_infos[2]
#         # print(bbox)
#         # print(image.shape)
#         loc_1 = (0 if (int(bbox[1]) - extend_pixel) < 0 else (int(bbox[1]) - extend_pixel))
#         loc_2 = (1036 if (int(bbox[3]) + extend_pixel) > 1036 else (int(bbox[3]) + extend_pixel))
#         loc_3 = (0 if (int(bbox[0]) - extend_pixel) < 0 else (int(bbox[0]) - extend_pixel))
#         loc_4 = (1380 if (int(bbox[2]) + extend_pixel) > 1380 else (int(bbox[2]) + extend_pixel))
#         bbox_image = image[loc_1:loc_2, loc_3:loc_4, :]
#         split_image_filename = os.path.join(temp_split_images, f'{num}.jpeg')
#         cv2.imwrite(split_image_filename, bbox_image)
#         split_images_list.append(split_image_filename)
#         num += 1
#     return split_images_list

# def get_area(mask):
#     area = 0
#     for i in mask:
#         i_list = i.tolist()
#         for element in i_list:
#             if element != 0:
#                 area += 1
#     return area

# # 得到分割区域的灰度值列表
# def get_result_gray(a, b):
#     result = []
#     for i in range(len(b)):
#         for j in range(len(b[i])):
#             if b[i][j] != 0:
#                 result.append(a[i][j])
#     return result

# # 通过灰度值列表，得到分数，目前的分数=分割区域的平均灰度
# def get_mean_gray_value(result_gray):
#     if len(result_gray) == 0:
#         return -1
#     mean_gray_value = np.mean(result_gray)
#     return mean_gray_value
#     # score = (result_gray_ave + result_gray_most)/2
#     # print(f'most:{result_gray_most}, score:{score}')
    

# def is_ash(area, mean_gray_value):
#     result = mean_gray_value - area*1.5
#     if result > ash_limit:
#         return result
#     else:
#         return False
    

# def get_classify_result(area, mean_gray_value):
#     if area < gray_area_limit:
#         if is_print:
#             print('面积小于100，可移除')
#         return 'can_remove'
    
#     if mean_gray_value < gray_limit_canremove:
#         return 'can_remove'
#     elif mean_gray_value > gray_limit_wuran:
#         return 'wuran'
#     else:
#         return None

    

# # filter_ash:default filter ash point
# def run(result, image, seg_model, filter_ash=True):
#     result_only_target_label = []
#     for single_bbox_info in result:
#         if single_bbox_info[0] in det_target_label:
#             result_only_target_label.append(single_bbox_info)
#     split_images_list = save_split_images(result_only_target_label, image)
#     seg_result = seg_model.run(split_images_list)
#     wuran_back_result = []
#     keyichu_back_result = []
    
#     for obj, split_image, ori_result in zip(seg_result, split_images_list, result_only_target_label):
#         if ori_result[0] == 0:
#             keyichu_back_result.append(ori_result)
#         elif ori_result[0] == 3:
#             wuran_back_result.append(ori_result)
#         elif ori_result[0] == 5:
#             wuran_back_result.append(ori_result)
#         # mask = np.uint8(obj[1])
#         # # print(mask)
#         # bbox_img = cv2.imread(split_image)
#         # if is_print:
#         #     save_seg_images(mask, bbox_img)
#         # bbox_img_gray = cv2.cvtColor(bbox_img, cv2.COLOR_BGR2GRAY)
#         # result_gray = get_result_gray(bbox_img_gray, mask)
#         # print(result_gray)
#         # mean_gray_value = get_mean_gray_value(result_gray)
#         # area = get_area(mask)
#     #     if mean_gray_value == -1:
#     #         if ori_result[0] == 0:
#     #             keyichu_back_result.append(ori_result)
#     #         elif ori_result[0] == 3:
#     #             wuran_back_result.append(ori_result)
#     #         elif ori_result[0] == 5:
#     #             wuran_back_result.append(ori_result)
#     #         continue
#     #     if filter_ash:
#     #         ash_result = is_ash(area, mean_gray_value)
#     #         if ash_result:
#     #             if is_print:
#     #                 print(f'{split_image}  灰点    score:{ash_result}')
#     #             continue
        
#     #     if ori_result[0] == 0:
#     #         keyichu_back_result.append(ori_result)
#     #     elif ori_result[0] == 3:
#     #         wuran_back_result.append(ori_result)
#     #     elif ori_result[0] == 5:
#     #         wuran_back_result.append(ori_result)
#         # back = get_classify_result(area, mean_gray_value)
#         # if is_print:
#         #     print(f'{split_image} {back}: area:{area}, mean_gray_value:{mean_gray_value}')
#         # if back == 'can_remove':
#         #     keyichu_back_result.append((can_remove, ori_result[1], ori_result[2]))
#         # elif back == 'wuran':
#         #     wuran_back_result.append((new_pollution, ori_result[1], ori_result[2]))
#         # elif back == None:
#         #     wuran_back_result.append(ori_result)
    
#     if is_print:
#         print('================================================================')
#         input()
#     # print('keyichu_back_result: ', keyichu_back_result)
#     # print('wuran_back_result: ', wuran_back_result)
#     return keyichu_back_result, wuran_back_result
        
# 0:pollution det_model
det_target_label = [0, 3, 5]
is_print = False
length_limit = 10





# filter_ash:default filter ash point
def run(result):
    wuran_back_result = []
    keyichu_back_result = []
    for object in result:
        label = object[0]
        if label not in det_target_label:
            continue
        x1, y1, x2, y2 = object[2]
        width = abs(x2 - x1)
        height = abs(y2 - y1)
        if width < length_limit and height < length_limit:
            if is_print:
                print(width, height)
            continue
        else:
            if label == 0:
                keyichu_back_result.append(object)
            elif label == 3:
                wuran_back_result.append(object)
            elif label == 5:
                wuran_back_result.append(object)
    if is_print:
        print('keyichu_back_result: ', keyichu_back_result)
        print('wuran_back_result:', wuran_back_result)
    return keyichu_back_result, wuran_back_result


if __name__ == '__main__':
    run()